import { DateTime } from 'luxon'
import {BaseModel, beforeCreate, BelongsTo, column, hasOne, HasOne} from '@ioc:Adonis/Lucid/Orm';
import uuid from 'uuid-v4'
import Skus from "App/Models/Skus";
import {belongsTo} from "@adonisjs/lucid/build/src/Orm/Decorators";
import Users from "App/Models/Users";
export default class Order extends BaseModel {
    @hasOne(() => Skus)
    public sku: HasOne<typeof Skus>
    public static table = 'tb_orders'
    @column({ isPrimary: true })
    public id: string
    /**
     * 订单归属用户
     */
    @column()
    public userId: string
    /**
     * 订单归属的商品(skuId)
     */
    @column()
    public skuId: string

    @column()
    public qty: number
    @column()
    public price: number
    /**
     * 创建时间
     */
    @column.dateTime({ autoCreate: true })
     public createdAt: DateTime
    /**
     * 最后更新时间
     */
     @column.dateTime({ autoCreate: true, autoUpdate: true })
     public updatedAt: DateTime

    /**
     *订单和用户关系
     */
     @belongsTo(() => Users)
     public user: BelongsTo<typeof Users>
     @beforeCreate()
     public static assignUuid(order: Order) {
         order.id = uuid()
     }
}
